package com.app.teaware.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.teaware.R;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.BaseInfoUpdateCache;
import com.app.teaware.bean.BaseListData;
import com.app.teaware.bean.BaseListDataListBean;
import com.app.teaware.bean.LoginBean;
import com.app.teaware.dialog.UpdateBaseInfoDialog;
import com.app.teaware.goods.GoodsListActivity;
import com.app.teaware.goods.PutUpGoodsActivity;
import com.app.teaware.greendao.GreenDaoUtils;
import com.app.teaware.stock.CheckWareActivity;
import com.app.teaware.stock.ResetWareActivity;
import com.app.teaware.util.MyUtils;
import com.app.teaware.ware.AllCheckListActivity;
import com.app.teaware.ware.ResetWareInfoActivity;
import com.app.teaware.ware.WareListActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.FileUtils;
import my.MySharedPreferences;

/**
 * 资讯首页
 */
public class HomeIndexActivity extends HomeBaseActivity {
    public final int GOTOGOODSACT = 48;


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    BaseListDataListBean mListBean;
    MyListAdapter mAdapter;



    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.home_index_activity);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        super.initData(savedInstanceState);
        initData();
        refreshData();
        FileUtils.deleteFile(Constants.DIR_TEMP);
        setTitle("WMS仓库管理系统");

//        EventBus.getDefault().register(this);
//        UpdateBean.check(mContext, false);
        checkCacheAndDownload();
    }

    private void initData() {
        mListBean = new BaseListDataListBean();
        BaseListData data;
        data = new BaseListData("99","80","出入库",R.drawable.base_img_default_square,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        

        data = new BaseListData("80","2","领料管理",R.drawable.icon_home_lj,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("80","3","退料管理",R.drawable.icon_home_tj,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("80","4","加工入库",R.drawable.icon_home_rk,R.drawable.base_img_default_square);
        mListBean.getList().add(data);


        data = new BaseListData("99","81","库存",R.drawable.base_img_default_square,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("81","6","库存核验",R.mipmap.icon_kchy,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("81","7","库存修改",R.mipmap.icon_kctz,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("81","8","库存查询",R.mipmap.icon_wms_check,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("81","10","批量核验",R.mipmap.icon_plhy,R.drawable.base_img_default_square);
        mListBean.getList().add(data);



        data = new BaseListData("99","82","库位",R.drawable.base_img_default_square,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
//        data = new BaseListData("82","8","库位盘点",R.mipmap.icon_check_ware,R.drawable.base_img_default_square);
//        mListBean.getList().add(data);
        data = new BaseListData("82","9","移库",R.mipmap.icon_reset_ware,R.drawable.base_img_default_square);
        mListBean.getList().add(data);

        
        data = new BaseListData("99","83","上架",R.drawable.base_img_default_square,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("83","5","货品上架",R.drawable.icon_home_sj,R.drawable.base_img_default_square);
        mListBean.getList().add(data);



    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);

    }


    @Override
    protected void onResume() {
        super.onResume();
//        QMUIStatusBarHelper.setStatusBarDarkMode(mContext);
    }


    @Override
    public void onDoubleClick() {
        super.onDoubleClick();
        refreshData();
    }

    void refreshData() {
        setGridEnterView();
    }
    

    void setGridEnterView() {
        if(mAdapter == null){
            mAdapter = new MyListAdapter();
            recyclerView.setAdapter(new MyListAdapter());
        }else {
            mAdapter.notifyDataSetChanged();
        }
    }



    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            refreshData();
        }
    }


    public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder> {

        public MyListAdapter() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public MyListAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_list_item, viewGroup, false);
            return new MyListAdapter.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(MyListAdapter.ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getParamList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_title)
            public TextView text_title;
            @BindView(R.id.recycler)
            public RecyclerView recycler;
            public MyChildAdapter myChildAdapter;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                BaseListData data = mListBean.getParamList().get(position);
                text_title.setText(data.getName());
                recycler.setLayoutManager(new GridLayoutManager(mContext, 3));
                myChildAdapter = new MyChildAdapter(mListBean.getChildListById(data.getId()));
                recycler.setAdapter(myChildAdapter);
            }


        }
    }

    public class MyChildAdapter extends RecyclerView.Adapter<MyChildAdapter.ViewHolder> {

        ArrayList<BaseListData> mData;
        public MyChildAdapter(ArrayList<BaseListData> mData) {
            this.mData = mData;
        }

        //创建新View，被LayoutManager所调用
        @Override
        public MyChildAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_grid_item, viewGroup, false);
            return new MyChildAdapter.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(MyChildAdapter.ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mData == null ? 0 : mData.size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_count)
            public TextView text_count;
            @BindView(R.id.img_icon)
            public ImageView img_icon;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                BaseListData data = mData.get(position);
                text_name.setText(data.getName());
                img_icon.setImageResource(data.getPath());
                list_item.setTag(data);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                BaseListData data = (BaseListData) v.getTag();
                MyUtils.putHomeSelectName(data.getName());
                MyUtils.putHomeSelectId(data.getId());
                switch (data.getId()){
                    case "2":
                    case "3":
                    case "4":
                        ActivityTool.skipActivity(mContext, GoodsListActivity.class);
                        break;
                    case "5": //货品上架
                        ActivityTool.skipActivity(mContext, PutUpGoodsActivity.class);
                        break;
                    case "6": //库存核验
                        ActivityTool.skipActivity(mContext, ResetWareInfoActivity.class);
                        break;
                    case "7": //库存修改
                        ActivityTool.skipActivity(mContext, WareListActivity.class);
                        break;
                    case "8":   //库存查询
                        ActivityTool.skipActivity(mContext, CheckWareActivity.class);
                        break;
                    case "9":   //移库
                        ActivityTool.skipActivity(mContext, ResetWareActivity.class);
                        break;
                    case "10":   //批量核验
                        ActivityTool.skipActivity(mContext, AllCheckListActivity.class);
                        break;

                }

            }
        }
    }


    void checkCacheAndDownload() {
        MySharedPreferences msp = new MySharedPreferences(mContext);
        if (msp.isFirst(LoginBean.getInstance().getAcode())) {
            GreenDaoUtils.deleteAll();
            BaseInfoUpdateCache beanNew  = new BaseInfoUpdateCache();
            UpdateBaseInfoDialog.toBaseInfoUpdateDialog(mContext, beanNew);

        }

    }




}
